﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Text;
using System.IO;

namespace IDAdmin.Lib
{
    /// <summary>
    /// Định dạng loại dữ liệu được export (bao gồm: Excel, Word, CSV)
    /// </summary>
    public enum ExportFormat : int
    {
        CSV = 1,
        Excel = 2,
        Word = 3
    };

    /// <summary>
    /// Lớp cung cấp hàm hỗ trợ export dữ liệu
    /// </summary>
    public class DataExporter
    {
        /// <summary>
        /// Export một đối tượng System.Web.UI.WebControls.Table sang file excel hoặc csv
        /// </summary>
        /// <param name="table">Đối tượng Table chứa dữ liệu được Export</param>
        /// <param name="format">Định dạng của file Export</param>
        /// <param name="fileName">Tên file (để Download)</param>
        public static void ExportTable(System.Web.UI.WebControls.Table table, ExportFormat format, string fileName)
        {
            try
            {
                HttpResponse response = HttpContext.Current.Response;
                response.ClearContent();
                response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
                if (format == ExportFormat.Excel)
                {
                    response.ContentType = "application/vnd.ms-excel";
                }
                else if (format == ExportFormat.Word)
                {
                    response.ContentType = "application/vnd.ms-word";
                }
                else
                {
                    response.ContentType = "text/csv";
                }
                StringWriter sw = new StringWriter();
                HtmlTextWriter htmltw = new HtmlTextWriter(sw);
                table.CellSpacing = 0;
                table.GridLines = GridLines.Both;
                table.RenderControl(htmltw);
                response.Write(sw.ToString());
                response.End();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Export dữ liệu được chứa trong 1 ASP.NET Control sang file excel/word/csv
        /// </summary>
        /// <param name="control"></param>
        /// <param name="format"></param>
        /// <param name="fileName"></param>
        public static void Export(System.Web.UI.WebControls.WebControl control, ExportFormat format, string fileName)
        {
            try
            {
                HttpResponse response = HttpContext.Current.Response;
                response.ClearContent();
                response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
                if (format == ExportFormat.Excel)
                {
                    response.ContentType = "application/vnd.ms-excel";
                }
                else if (format == ExportFormat.Word)
                {
                    response.ContentType = "application/vnd.ms-word";
                }
                else
                {
                    response.ContentType = "text/csv";
                }
                StringWriter sw = new StringWriter();
                HtmlTextWriter htmltw = new HtmlTextWriter(sw);
                control.RenderControl(htmltw);
                response.Write(sw.ToString());
                response.End();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
